package com.biyesheji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.biyesheji.domain.*;
import com.biyesheji.dao.BusinessMapper;
import com.biyesheji.service.IBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biyesheji.service.IOrdersService;
import com.biyesheji.service.IWaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author 崔
 * @since 2023-02-12
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService {


    @Autowired
    IOrdersService ordersService;

    @Autowired
    IWaresService waresService;

    @Override
    public List getBusinessList(String businessPhone) {

        Long aLong = Long.valueOf(businessPhone);

        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(businessPhone != null && aLong != -1, Business :: getPhone, businessPhone);
        queryWrapper.like(businessPhone != null && !businessPhone.equals("-1"), Business :: getPhone, businessPhone);


        List<Business> list = this.list(queryWrapper);


        return list;
    }

    @Override
    public List<Business> getBusiness(Long id, String businessPhone, Integer status) {

        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, Business::getId, id);
        queryWrapper.like(businessPhone != null && !businessPhone.equals("-1"), Business::getPhone, businessPhone);
        queryWrapper.eq(status != null && status != 3, Business::getStatus, status);

        List<Business> businessesList = this.list(queryWrapper);
        return businessesList;
    }



    @Override
    public Boolean updateBusiness(Business business) {
        this.updateById(business);

        if (business.getStatus() == 0){

            LambdaQueryWrapper<Wares> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Wares::getCreatePhone, business.getPhone());

            List<Wares> waresList = waresService.list(queryWrapper1);

            Wares wares1 = new Wares();
            wares1.setStatus(0);

            waresService.update(wares1, queryWrapper1);

            if (waresList != null && waresList.size() != 0){

                for (int i = 0; i < waresList.size(); i++) {

                    Wares wares = waresList.get(i);
                    LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq( Orders::getWaresId, wares.getId());

                    Orders orders = new Orders();
                    orders.setStatus(0);
                    ordersService.update(orders, queryWrapper);

                }


            }



        }


        return true;
    }


}
